<template>
    <el-dialog
        :title="!dataForm.id ? $t('handle.add') : $t('handle.update')"
        :close-on-click-modal="false"
        :visible.sync="visible"
        >
    <div v-loading = loading>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="标签名" prop="label">
        <el-input v-model="dataForm.label" placeholder="标签名"></el-input>
      </el-form-item>
      <el-form-item label="数据值" prop="value">
        <el-input v-model="dataForm.value" placeholder="数据值"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型"></el-input>
      </el-form-item>
      <el-form-item label="是否系统字典" size="mini" prop="isSystem">
        <el-radio-group v-model="dataForm.isSystem">
          <el-radio :label="1">{{ $t('status.yes') }}</el-radio>
          <el-radio :label="2">{{ $t('status.no') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('status.status')" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{ $t('status.enable') }}</el-radio>
          <el-radio :label="2">{{ $t('status.disabled') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tabble.sort')" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="父级编码" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="父级编码"></el-input>
      </el-form-item>
    </el-form>
    <div style="text-align:right;">
    <span slot="footer" class="dialog-footer">
      <el-button @click="callOf()">{{ $t('handle.cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('handle.confirm') }}</el-button>
    </span>
    </div>
    </div>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      loading:false,
      visible: false,
      dataForm: {
        id: "",
        label: "",
        value: "",
        type: "",
        isSystem: "",
        status: "",
        description: "",
        sort: "",
        parentId: "",
        createBy: "",
        updateBy: ""
      },
      dataRule: {
        label: [{ required: true, message: "标签名不能为空", trigger: "blur" }],
        value: [{ required: true, message: "数据值不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }]
      }
    };
  },
  activated() {
    this.getAllDict();
  },
  methods: {
    callOf() {
      this.visible = false;
      this.$refs[`dataForm`].resetFields();
    },
    clearForm() {
      this.dataForm.label ='';
      this.dataForm.value = '';
      this.dataForm.type = '';
      this.dataForm.isSystem = '';
      this.dataForm.status = '';
      this.dataForm.description = '';
      this.dataForm.sort = '';
      this.dataForm.parentId = '';
      this.dataForm.updateBy = '';
      this.dataForm.createBy = '';
    },
    init(id) {
      this.visible = true;
      this.dataForm.id = id || 0;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      })
      if (typeof id == "undefined") {
        // 新增
        this.clearForm();
      }
      if (this.dataForm.id) {
        this.$http({
          url: `/dg-ic/ic/dict/info/${this.dataForm.id}`,
          method: "get",
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataForm.label = data.data.label;
            this.dataForm.value = data.data.value;
            this.dataForm.type = data.data.type;
            this.dataForm.isSystem = data.data.isSystem;
            this.dataForm.status = data.data.status;
            this.dataForm.description = data.data.description;
            this.dataForm.sort = data.data.sort;
            this.dataForm.parentId = data.data.parentId;
            this.dataForm.updateBy = data.data.updateBy;
            this.dataForm.createBy = data.data.createBy;
          }
        });
      }
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate(validate => {
        if (validate) {
          this.loading = true;
          this.$http({
            url: `/dg-ic/ic/dict/${!this.dataForm.id ? "save" : "update"}`,
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              label: this.dataForm.label,
              value: this.dataForm.value,
              type: this.dataForm.type,
              isSystem: this.dataForm.isSystem,
              status: this.dataForm.status,
              description: this.dataForm.description,
              sort: this.dataForm.sort,
              parentId: this.dataForm.parentId,
              createBy: this.$store.state.user.id,
              updateBy: this.$store.state.user.id
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.loading = false;
                  this.visible = false;
                  this.$refs[`dataForm`].resetFields();
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
              this.loading = false;
            }
          });
        }
      });
    },
    // 获取所有未删除的字典
    getAllDict() {
      this.$http({
        url: this.$http.adornUrl(`ic/dict/getAllDict`),
        method: "get",
        params: this.$http.adornParams
      }).then(({ data }) => {
        if (data && data.code === 0) {

        }
      });
    }
  }
};
</script>
